// 1.工程化
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

// 2.创建路由对象并配置路由
const router = new VueRouter({
    routes: [
        {
            name: 'login',
            path: '/login',
            component: () => import('@/views/login.vue')
        },
        {
            name: 'register',
            path: '/register',
            component: () => import('@/views/register.vue')
        },
        {
            name: 'pesonal',
            path: '/pesonal/:id',
            // path: '/pesonal',
            component: () => import('@/views/pesonal.vue')
        },
        ,
        {
            name: 'editeInfo',
            path: '/editeInfo/:id',
            // path: '/pesonal',
            component: () => import('@/views/editeInfo.vue')
        },
    ]
})
const arr = ['pesonal']

router.beforeEach((to, from, next) => {
    // console.log(to.name)

    //是否是去个人中心页
    if (arr.indexOf(to.name) !== -1) {
        // console.log(123);
        //判断是否登陆了
        let token = JSON.parse(localStorage.getItem('heima_token'))
        // console.log(token.token);
        if (token.token) {
            next()
            // 如果没登陆重定向到登陆页
        } else {
            next({ name: 'login' })
        }
    } else {
        next()
    }


})
// router.beforeEach((to, from, next) => {
//     // console.log(to.name)
//     // console.log(arr.indexOf(to.name));
//     // next()
//     if (arr.indexOf(to.name) !== -1) {
//         // console.log(123);
//         let token = localStorage.getItem('heima_token')
//         if (token) {
//             next()
//         } else {
//             next({ name: 'login' })
//         }
//     } else {
//         next()
//     }
// })

// 3.暴露
export default router